//
//  QASquareViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/10.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "QASquareCell.h"
#import "QASectionSegView.h"

#import "QADetailViewController.h"

#import "QASquareViewController.h"

@interface QASquareViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) QASectionSegView *headerView;

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, strong) NSMutableArray <CommonQAModel *>*dataArray;

@property (nonatomic, copy) NSString * remark;

@end

@implementation QASquareViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    
    self.remark = @"2";
    [self refreshParam];
    [self request_QAList];
}

- (void)refreshParam {
    self.pageSize = 10;
    self.pageNum = 1;
}

- (void)initView {
    
    self.title = @"问答广场";
    [self.view addSubview:self.tableview];
    
    self.headerView.frame = CGRectMake(0, 0, TTScreenWidth, 40);
    self.tableview.tableHeaderView = self.headerView;
    self.headerView.autoresizingMask = UIViewAutoresizingNone;
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    WEAKSELF
    self.headerView.QAListHeaderViewBlock = ^(QAListHeaderActionType type) {
      
        switch (type) {
            case QAListHeaderActionType_hot:{
                
                weakSelf.remark = @"2";
                [weakSelf refreshParam];
                [weakSelf request_QAList];
                
            } break;
            case QAListHeaderActionType_new:{
                
                weakSelf.remark = @"1";
                [weakSelf refreshParam];
                [weakSelf request_QAList];
                
            } break;
            default:
                break;
        }
    };
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return self.dataArray.count;
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [QASquareCell calculateHeightWithModel:self.dataArray[indexPath.row]];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QASquareCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QASquareCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillWithModel:self.dataArray[indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    QADetailViewController *vc = [[QADetailViewController alloc] init];
    vc.mqId = self.dataArray[indexPath.row].mqId;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"QASquareCell" bundle:nil] forCellReuseIdentifier:@"QASquareCellID"];
        
        WEAKSELF
        _tableview.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            [weakSelf refreshParam];
            [weakSelf request_QAList];
        }];
        
        _tableview.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            weakSelf.pageNum ++;
            [weakSelf request_QAList];
        }];
        
        _tableview.mj_footer.ignoredScrollViewContentInsetBottom = Is_IPhoneX ? Bottom_SafeHeight : CGFLOAT_MIN;

    }
    return _tableview;
}

- (QASectionSegView *)headerView {
    if (!_headerView) {
        _headerView = [[NSBundle mainBundle] loadNibNamed:@"QASectionSegView" owner:nil options:nil].firstObject;
    }
    return _headerView;
}

- (void)request_QAList {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    NSMutableDictionary *t = @{}.mutableCopy;
    
    [dic setValue:@(self.pageNum) forKey:@"pageNum"];
    [dic setValue:@(self.pageSize) forKey:@"pageSize"];
    
    [t setValue:@"2" forKey:@"isUp"];
    [t setValue:self.remark forKey:@"isHost"];    
    [dic setValue:t forKey:@"t"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_MYQA];
    
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        [[TTNoResultManager shareManager] hideNoResultWithView:weakSelf.tableview];
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                
                if ([resultDic isKindOfClass:[NSDictionary class]]) {
                    NSArray *list = [resultDic valueForKey:@"list"];
                    if ([list isKindOfClass:[NSArray class]]) {
                        if (weakSelf.pageNum == 1) {
                            weakSelf.dataArray = @[].mutableCopy;
                        }

                        for (NSDictionary *listDic in list) {
                            CommonQAModel *model = [CommonQAModel modelWithDictionary:listDic];
                            
                            NSArray *userAnswerList = [listDic valueForKey:@"userAnswerList"];
                            if ([userAnswerList isKindOfClass:[NSArray class]]) {
                                model.userAnswerList = @[].mutableCopy;
                                for (NSDictionary *aswDic in userAnswerList) {
                                    
                                    CommonAnswerModel *aswModel = [CommonAnswerModel modelWithDictionary:aswDic];
                                    [model.userAnswerList addObject:aswModel];
                                }
                            }                            
                            
                            [weakSelf.dataArray addObject:model];
                        }

                        [weakSelf.tableview reloadData];
                    }
                }
            }
        }
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

@end
